<template>
  <div>
    <title-panel />
    <van-form
      input-align="right"
      error-message-align="right"
      :show-error-message="false"
      show-error
      scroll-to-error
      validate-first
      @failed="onFailed"
      @submit="onSubmit"
    >
      <van-field
        readonly
        :value="obj.startTime"
        name="startTime"
        label="开始使用时间"
        placeholder="请选择开始时间"
        input-align="right"
        required
        is-link
        @click="showPicker1 = true"
        :rules="[{ required: true, message: '请选择开始时间' }]"
      />
      <van-popup v-if="!isDisabled" v-model="showPicker1" position="bottom">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          @confirm="onConfirmStartDate"
          @cancel="showPicker1 = false"
        />
      </van-popup>
      <van-field
        readonly
        :value="obj.finishTime"
        name="finishTime"
        label="停止使用时间"
        placeholder="请选择停止时间"
        input-align="right"
        required
        is-link
        @click="showPicker2 = true"
        :rules="[{ required: true, message: '请选择停止时间' }]"
      />
      <van-popup v-if="!isDisabled" v-model="showPicker2" position="bottom">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          @confirm="onConfirmStartDate2"
          @cancel="showPicker2 = false"
        />
      </van-popup>
      <van-field
        readonly
        name="feedName"
        label="饲料名称"
        :value="obj.feedName"
        is-link
        required
        placeholder="点击跳转选择，单选"
        input-align="right"
        :rules="[{ required: true, message: '请选择饲料名称' }]"
        @click="goFodder"
      />
      <van-field
        v-model="obj.dosage"
        name="dosage"
        label="用量（kg）"
        required
        input-align="right"
        placeholder="点击填写"
        :rules="[{ required: true, message: '请选择用量' }]"
      />
      <div class="spacing" />
      <div class="add-btn">
        <van-button
          block
          type="info"
          color="#37B5B5"
          style="width: 85%; margin: auto; margin-top: 50px"
          native-type="submit"
          size="large"
          :loading="loading"
          loading-text="提交中..."
          >保存</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import {
  Form,
  Field,
  Calendar,
  RadioGroup,
  Radio,
  Dialog,
  Toast,
  DatetimePicker,
  Picker,
  Button,
  Icon,
  Popup,
  Cell
} from 'vant'
import UploadImg from '@/components/uploadImg'
import TitlePanel from '@/components/titlePanel'
import dayjs from 'dayjs'
import { getValue, setValue, clearValue } from '@/utils/auth'
import { getHandle, postHandle } from '../../api'
export default {
  components: {
    UploadImg,
    TitlePanel,
    [Form.name]: Form,
    [Field.name]: Field,
    [Calendar.name]: Calendar,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [Button.name]: Button,
    [Icon.name]: Icon,
    [Popup.name]: Popup,
    [Toast.name]: Toast,
    [Cell.name]: Cell,
    [Dialog.Component.name]: Dialog.Component
  },
  data() {
    return {
      contentList: '',
      fileInfoList: [],
      currentDate: new Date(),
      showPicker1: false,
      showPicker2: false,
      loading: false,
      flag: getValue('swBj') || '',
      //   typeList: [],
      //   resultList: [],
      isDisabled: false,
      natureShows: false,
      natureNameList: [],
      siteList: [],
      natureShow: false,
      natureNameLists: [],
      fertiliList: [],
      houseInfoId: getValue('fzmHourseId') || '',
      productName: {},
      obj: {
        startTime: '',
        finishTime: '',
        feedName: '',
        dosage: '',
        forageId: ''
      },
      contentObj: {},
      type: null
    }
  },
  created() {},
  mounted() {
    this.$bridge.register('refresh', data => {
      if (!!getValue('swObj')) {
        this.obj = JSON.parse(getValue('swObj'))
      }
      if (!!getValue('productName')) {
        this.productName = JSON.parse(getValue('productName'))
        this.obj.feedName = this.productName.name
        this.obj.forageId = this.productName.id
      }
    })
  },
  methods: {
    goFodder() {
      setValue('swFlag', 'swAdd')
      setValue('swObj', JSON.stringify(this.obj))
      this.$bridge.call('selectProducts')
    },
    //获取默认数据
    initialize(id) {},
    //开始时间
    onConfirmStartDate(value) {
      let timestamp = dayjs(value).valueOf()
      if (timestamp <= dayjs(this.obj.finishTime || '2031-12-31').valueOf())
        this.obj.startTime = dayjs(value).format('YYYY-MM-DD')
      else Toast('不得晚于停止时间')
      this.showPicker1 = false
    },
    //结束时间
    onConfirmStartDate2(value) {
      let timestamp = dayjs(value).valueOf()
      if (timestamp >= dayjs(this.obj.startTime || '1987-1-1').valueOf())
        this.obj.finishTime = dayjs(value).format('YYYY-MM-DD')
      else Toast('不得早于开始时间')
      this.showPicker2 = false
    },
    onFailed(errorInfo) {
      Toast(errorInfo.errors[0].message)
    },
    onSubmit(value) {
      this.loading = true
      console.log(value)
      let params = {
        houseInfoId: this.houseInfoId,
        useStartDate: value.startTime,
        useEndDate: value.finishTime,
        forageId: this.obj.forageId,
        forageName: value.feedName,
        dosage: value.dosage,
        subjectInfoId: getValue('subjectInfoId'),
        tenantCode: getValue('tenantCode')
      }
      postHandle('/agri/api/yzbzh/feedingRecord/save', params).then(res => {
        if (res.status == '0') {
          this.$toast(res.msg)
          clearValue('productName')
          clearValue('swObj')
          this.$bridge.call('base.closePage')
        } else {
          Toast.fail(res.msg | '操作失败')
        }
        setTimeout(() => {
          this.loading = false
        }, 1000)
      })
      // let imgInfos = this.obj.imgInfos.map((item) => item.url).join(",");
    }
  }
}
</script>
<style lang="scss" scoped>
.tip {
  padding-top: 15px;
  color: #999999;
  font-size: 12px;
  line-height: 16px;
  margin: auto;
}
.btn {
  width: 80%;
  margin: 50px auto 0 auto;
}
.btn > div {
  height: 50px;
  background: #2871fe;
  border-radius: 4px;
  text-align: center;
  line-height: 50px;
  color: #ffffff;
  font-size: 17px;
}
.btn .cancel {
  margin-top: 20px;
  background: rgba(40, 113, 254, 0.1);
  border: 1px solid #2871fe;
  color: #2871fe;
}
::v-deep .van-field__label {
  width: 6em;
}
.bthStyle {
  width: 56px;
  height: 20px;
  border-radius: 10px;
  border: 1px solid #2871fe;
}
.active {
  background-color: #fff;
  color: #2871fe;
}
.add-btn {
  padding-bottom: 20px;
}
.addbtn {
  width: 50px;
  height: 24px;
  font-size: 13px;
  padding: 0;
  margin-right: 13px;
  z-index: 10;
  float: right;
  margin-top: -38px;
}
.btnfrom {
  padding: 14px 0;
}
.fromInput {
  width: 86%;
}
</style>
